﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Aribaccio;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// ParameterLimitEditorDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class ParameterLimitEditorDialog : Window, IDialog<MParameterLimitData>
    {
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        protected bool isEnabledLimitTo;
        /// <summary>
        /// 显示标题
        /// </summary>
        [ObservableProperty]
        protected string titleName = "参数界限编辑";
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 当前编辑参数界限数据
        /// </summary>
        [ObservableProperty]
        protected MParameterLimitData? parameterLimit;
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public MParameterLimitData? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ParameterLimitEditorDialog()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ParameterLimitEditorDialog(string argTitleName, MParameterLimitData argParameterLimit)
            : this()
        {
            this.TitleName = string.Concat(this.TitleName, "-", argTitleName);
            this.ParameterLimit = argParameterLimit;
            if (argParameterLimit.TargetSlave != null && argParameterLimit.TargetSlave.DataType != DataType.String)
            {
                this.IsEnabledLimitTo = true;
            }
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (this.ParameterLimit == null || !this.Check()) return;
            this.DialogResult = true;
            this.DialogResultData = this.ParameterLimit;
            this.DialogResultData.LimitTime = Convert.ToInt32(this.ParameterLimit.LimitTimeDisplay);
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 检测
        /// </summary>
        /// <returns></returns>
        protected bool Check()
        {
            if (this.ParameterLimit == null) return true;
            if (string.IsNullOrWhiteSpace(this.ParameterLimit.LimitTimeDisplay))
            {
                this.StatusMsg = "检测时长不能为空";
                return false;
            }
            if (!int.TryParse(this.ParameterLimit.LimitTimeDisplay, out int nLimitTime) || nLimitTime < 1)
            {
                this.StatusMsg = "检测时长必须正数，并大于等于1";
                return false;
            }
            if (this.IsEnabledLimitTo)
            {
                if (!string.IsNullOrWhiteSpace(this.ParameterLimit.Limit) && !decimal.TryParse(this.ParameterLimit.Limit, out _))
                {
                    this.StatusMsg = "界限/下限必须数值";
                    return false;
                }

                if (!string.IsNullOrWhiteSpace(this.ParameterLimit.LimitTo) && !decimal.TryParse(this.ParameterLimit.LimitTo, out _))
                {
                    this.StatusMsg = "界限/上限必须数值";
                    return false;
                }
            }
            return true;
        }
    }
}
